package dao;

import helper.ReadConfig;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import beans.FAQbean;

public class FAQ {

    public boolean createFAQ(FAQbean faqBean) {
        try {
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "insert into tbl_FAQ"
                    + " values('" + faqBean.getFaq_question() + "',"
                    + "'" + faqBean.getFaq_answer() + "')";
            System.out.println(sSQL);
            Statement stm = con.createStatement();
            int rs = stm.executeUpdate(sSQL);
            System.out.println(rs);
            if (rs == 1) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //
    public boolean updateFAQ(FAQbean faqBean) {
        try {
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "Update tbl_FAQ set "
                    + "   faq_question='" + faqBean.getFaq_question()
                    + "', faq_answer='" + faqBean.getFaq_answer()
                    + "'  WHERE faq_id = " + faqBean.getFaq_id();
            System.out.println(sSQL);
            Statement stm = con.createStatement();
            int rs = stm.executeUpdate(sSQL);
            System.out.println(rs);
            if (rs == 1) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //
    public boolean deleteFAQ(String FAQ_id) {
        try {
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "DELETE FROM tbl_FAQ WHERE FAQ_Id = " + Integer.parseInt(FAQ_id);
            Statement stm = con.createStatement();
            int rs = stm.executeUpdate(sSQL);
            if (rs == 1) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //
    public List<FAQbean> showFAQ() {
        try {
            List<FAQbean> listFAQ = new ArrayList<FAQbean>();
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "select * from tbl_FAQ";
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sSQL);
            while (rs.next()) {
                FAQbean faq = new FAQbean();
                faq.setFaq_id(rs.getString(1));
                faq.setFaq_question(rs.getString(2));
                faq.setFaq_answer(rs.getString(3));
                listFAQ.add(faq);
            }
            rs.close();
            stm.close();
            return listFAQ;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<FAQbean> showFAQById(String FAQ_id) {
        try {
            List<FAQbean> list = new ArrayList<FAQbean>();
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "select * from tbl_FAQ where FAQ_Id= " + Integer.parseInt(FAQ_id);
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sSQL);
            while (rs.next()) {
                FAQbean faq = new FAQbean();
                faq.setFaq_id(rs.getString(1));
                faq.setFaq_question(rs.getString(2));
                faq.setFaq_answer(rs.getString(3));
                list.add(faq);
            }
            rs.close();
            stm.close();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
